<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;

class UserController extends Controller
{
    //用户的添加
    public function getAdd(){
        // dd(111);
    	return view('admin.user.add');
    }
    public function postInsert(Request $request)
    {
    	// 验证表单提交信息
    	$this->validate($request, [
	        'username' => 'required|regex:/^\S{8,18}$/',
	        'password'=>'required|regex:/^\S{6,20}$/|same:reupwd',
            'email'=>'required',
	        'profile'=>'required',

    	],[
    		'username.required'=>'用户名不能为空',
    		'username.regex'=>'用户名格式不正确',
    		'password.required'=>'密码不能为空',
    		'password.regex'=>'密码格式不正确',
    		'password.same'=>'两次密码不一致',
            'email.required'=>'邮箱不能为空',
    		'profile.required'=>'请上传头像',
    	]);
    	// 去掉信息中无用的信息
    	$data = $request->except('_token','reupwd');
    	// 密码加密
    	$data['password'] = Hash::make($data['password']);
    	// 处理头像
    	if($request->hasFile('profile')){
    		$fileName = time().rand(100000,999999);
    		$suffix = $request->file('profile')->getClientOriginalExtension();
    		$fileName = $fileName.'.'.$suffix;
    		$request->file('profile')->move('./Uploads',$fileName);
    		$data['profile'] = '/Uploads/'.$fileName;
    	
    	}
    	
    	// 插入数据库
    	$res = DB::table('shop_users')->insert($data);

    	if($res){
    		return redirect('/admin/user/list')->with('info','添加成功');
    	}else{
    		return back()->withInput();
    	}
    }
    //用户列表
    public function getList(Request $request)
    {
       
        // 获取关键字内容
       
        // 分页获取用户信息
         $users = DB::table('shop_users')->where(function($query) use ($request){
            $k = $request->input('keyword');
            if(!empty($k)){
                $query->where('username','like','%'.$k.'%');
             } 
         })->paginate($request->input('num',5));
         // 分配变量  解析模板
         
         return view('admin.user.list',['users'=>$users,'request'=>$request]);
    }

    // 用户的修改
    public function getEdit(Request $request)
    {
        // dd(2222);
        // 获取用户的id
        $id = $request->input('id');
        // 获取id的信息
        $user = DB::table('shop_users')->where('id',$id)->first();
        return view('admin.user.edit',['user'=>$user]);
    }

    // 用户的更新
    public function postUpdata(Request $request)

    {
       // 表单验证
       
       // 处理图像
      $data = $request->except(['_token','id']);

      $path = $this->upload($request);
        if(!empty($path)){
            $data['profile'] = $path;
        }
       // 更新
       // dd($data);
       $res = DB::table('shop_users')->where('id',$request->input('id'))->update($data);

      if($res){
        return redirect('/admin/user/list')->with('info','修改成功');
      }else{
        return back()->with('error','修改失败');
      }

    }

    // 删除
    public function getDelete(Request $request)
    {
        $id = $request->input('id');
        // 执行删除
        $res = DB::table('shop_users')->where('id',$id)->delete();
        if($res){
            return back()->with('info','删除成功');

        }else{
            return back()->with('error','删除失败');
        }
    }

    public function upload($request)
    {
        if($request->hasFile('profile')){
            $fileName = time().rand(100000,999999);
            $suffix = $request->file('profile')->getClientOriginalExtension();
            $fileName = $fileName.'.'.$suffix;
            $request->file('profile')->move('./Uploads',$fileName);
            $data['profile'] = '/Uploads/'.$fileName;
            return $data['profile'];
        }

    }

    /*================================前台=========================================*/
    //显示用户信息
    public function getInfo()
    {
        $data = DB::table('shop_users')->where('id',session('id'))->first();
        return view('home.user.info',['data'=>$data]);
    }

    //修改用户信息
    public function postEdit(Request $request)
    {
        $data = $request->except('_token');
        // dd($data);
        $path = $this->upload($request);
        if(!empty($path)){
            $prevPath = DB::table('shop_users')->where('id',session('id'))->select('profile')->first()->profile;
            $data['profile'] = $path;
        }
        $res = DB::table('shop_users')->where('id',session('id'))->update($data);
        // dd($res);
        if($res){
            unlink('.'.$prevPath);
            return back()->with('info','资料修改成功');
        }else{
            return back()->with('info','资料修改失败');
        }
    }

    //修改密码
    public function getPwdedit(Request $request)
    {
        //检测是否已设置密保 没设置 跳转去设置,  已设置 继续向下
        $res = DB::table('shop_mibao')->where('user_id',session('id'))->first();
        if(!$res){
            return redirect('/home/user/addmibao');
        }

        return view('home.user.pwdedit',['data'=>$res]);
    }

    //执行修改
    public function postDopwdedit(Request $request)
    {
        $data = $request->except('_token');
        // dd($data);
        //获取该用户密保问题及答案
        $quesans = DB::table('shop_mibao')->where('user_id',session('id'))->first();

        if($quesans->answer1 == $data['answer1'] && $quesans->answer2 == $data['answer2'] && $quesans->answer3 == $data['answer3']){
            return view('home.reset',['id'=>session('id')]);
        }else{
            return back()->with('info','回答错误');
        }


    }

    //添加密保
    public function getAddmibao(Request $request)
    {
        return view('home.user.mibao');

    }

    //执行添加密保
    public function postDoaddmibao(Request $request)
    {
        $data = $request->except('_token');
        $data['user_id'] = session('id');
        // dd($data);
        $res = DB::table('shop_mibao')->insert($data);
        if($res){
            return redirect('/home/user/pwdedit');
        }else{
            return back()->with('info','设置失败,请重新设置');
        }
    }

    //用户收藏
    public function getCollection(Request $request)
    {
        $goods_id = DB::table('shop_collection')->where('user_id',session('id'))->select('goods_id')->get();
        // dd($goods_id);
        $data = [];
        foreach($goods_id as $k=>$v){
            $info = DB::table('shop_goods')->where('id',$v->goods_id)->select('title','price')->first();
            $info->path = DB::table('shop_goopics')->where('goods_id',$v->goods_id)->first()->path;
            $info->goods_id = $v->goods_id;
            $data[] = $info;
        }
        // dd($data);
        return view('home.user.collection',['data'=>$data]);
    }

    //删除收藏
    public function getDelcollection(Request $request)
    {
        $id = $request->input('id');
        // dd($id);

        $res = DB::table('shop_collection')->where('user_id',session('id'))->where('goods_id',$id)->delete();
        if($res){
            return 1;
        }else{
            return 0;
        }
    }
}
